// Copyright 2006 PR1ME. All Rights Reserved.
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;

public class BitReader {

  public BitReader(DataInputStream input) {
    this.input = input;
  }

  public boolean read() throws IOException {
    if (pos == buf.length) {
      int bytesRead = input.read(buf);
      if (bytesRead < 0) {
        throw new EOFException();
      }
      pos = buf.length - bytesRead;
      System.arraycopy(buf, 0, buf, pos, bytesRead);
    }

    boolean result = (buf[pos] & bitmask) != 0;

    if (bitmask == 0x80) {
      advance();
    } else {
      bitmask <<= 1;
    }

    return result;
  }

  private void advance() {
    bitmask = 0x1;
    ++pos;
  }

  private int bitmask = 0x1;
  private final byte[] buf = new byte[1024];
  private DataInputStream input;
  private int pos = 1024;
}
